<?php

if (!defined('RAIZ')) define('RAIZ', $_SERVER['DOCUMENT_ROOT']. '/neumo/');  

require_once RAIZ . 'db/db_mysql.php';

$page = $_POST['page']; // get the requested page
$limit = $_POST['rows']; // get how many rows we want to have into the grid
$sidx = $_POST['sidx']; // get index row - i.e. user click to sort
$sord = $_POST['sord']; // get the direction
$bsc_palabra = isset($_POST['searchString']) ? $_POST['searchString'] : ''; 
$bsc_campo= isset($_POST['searchField']) ? $_POST['searchField'] : ''; 
$bsc_Oper= isset($_POST['searchOper']) ? $_POST['searchOper'] : ''; 

$responce = new stdClass;

if (!$sidx) $sidx = 1;

$db_usuario= new dbase_usuario;
//$db_usuario->abre();

$result = mysql_query("SELECT COUNT(*) AS count FROM usuarios WHERE 1 ");

$row = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $row['count'];

if ($count > 0) {
  $total_pages = ceil($count / $limit);
} else {
  $total_pages = 0;
}
if ($page > $total_pages) $page = $total_pages;

$start = $limit * $page - $limit; // do not put $limit*($page - 1)
$start= max($start,0);
$filtro= convierte_operador_jquery($bsc_campo,$bsc_Oper,$bsc_palabra);

$sql_limite= $start . ", " . $limit;
$sql_order=  $sidx . " ". $sord;
$sql_campos=" 
  id_usuario, 
  apellidos, 
  nombre,
  descripcion,
  es_bloqueo,
  solicitud_socio,
  correo_valido,
  es_socio,
  es_area,
  es_congreso,
  es_admin
  ";

$sql= "SELECT " . $sql_campos . " FROM usuarios, cdfcargo WHERE (cargo = id) and " . $filtro . " ORDER BY " . $sql_order . " LIMIT " . $sql_limite; 

$result = mysql_query($sql) or die("Error en la SQL de usuarios" . mysql_error());

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $responce->rows[$i]['id'] = $row['id_usuario'];
  $responce->rows[$i]['cell'] = array($row['id_usuario'], $row['apellidos'], $row['nombre'], 
      $row['descripcion'],$row['es_bloqueo'],$row['solicitud_socio'],$row['correo_valido'],$row['es_socio'],
      $row['es_area'],$row['es_congreso'], $row['es_admin']);
  $i++;
}

$db_usuario->cierra();

echo json_encode($responce);


/*==============================================================================
 * Subrutinas
 */


?>